/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_frcmd_filter.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/12 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <protobuf-c/protobuf-c.h>

#include "../../comm/frcmd_parse_detail.h"
#include "../../comm/client.pb-c.h"
#include "../../comm/frcmd_parse.h"
#include "../../comm/apm.pb-c.h"
#include "../../comm/frcmd_client.h"
#include "../../comm/frcomm.h"
#include "../../comm/frmem.h"
#include "../../comm/frsutl.h"
#include "../../comm/apmtest_cunit.h"

extern int g_i_port;
extern char* g_p_server;

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}
int __init_socket(const char *p_addr, unsigned short us_port, int *p_sockfd);

void case1(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("add") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","add");

    //p_ip_proto
    i_size_tmp = strlen("tcp") + 1;
    p_ip_proto = frmalloc(i_size_tmp);
    snprintf(p_ip_proto,i_size_tmp,"%s","tcp");

    //p_src_iprange_portrange
    i_size_tmp = strlen("192.168.1.2-192.168.1.30|80-100") + 1;
    p_src_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_src_iprange_portrange,i_size_tmp,"%s","192.168.1.2-192.168.1.30|80-100");

    //p_dst_iprange_portrange
    i_size_tmp = strlen("192.168.0.2-192.168.0.30|80-100") + 1;
    p_dst_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_dst_iprange_portrange,i_size_tmp,"%s","192.168.0.2-192.168.0.30|80-100");

    //p_mac
    i_size_tmp = strlen("00:ff:00:d5:44:a2") + 1;
    p_mac = frmalloc(i_size_tmp);
    snprintf(p_mac,i_size_tmp,"%s","00:ff:00:d5:44:a2");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_ip_proto);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}

void case2(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("add") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","add");

    //p_src_iprange_portrange
    i_size_tmp = strlen("192.168.1.2-192.168.1.30|80-100") + 1;
    p_src_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_src_iprange_portrange,i_size_tmp,"%s","192.168.1.2-192.168.1.30|80-100");

    //p_dst_iprange_portrange
    i_size_tmp = strlen("192.168.0.2-192.168.0.30|80-100") + 1;
    p_dst_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_dst_iprange_portrange,i_size_tmp,"%s","192.168.0.2-192.168.0.30|80-100");

    //p_mac
    i_size_tmp = strlen("00:ff:00:d5:44:a2") + 1;
    p_mac = frmalloc(i_size_tmp);
    snprintf(p_mac,i_size_tmp,"%s","00:ff:00:d5:44:a2");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}

void case3(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("add") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","add");

    //p_ip_proto
    i_size_tmp = strlen("tcp") + 1;
    p_ip_proto = frmalloc(i_size_tmp);
    snprintf(p_ip_proto,i_size_tmp,"%s","tcp");

    //p_src_iprange_portrange
    i_size_tmp = strlen("192.168.1.2-192.168.1.30|80-100") + 1;
    p_src_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_src_iprange_portrange,i_size_tmp,"%s","192.168.1.2-192.168.1.30|80-100");

    //p_mac
    i_size_tmp = strlen("00:ff:00:d5:44:a2") + 1;
    p_mac = frmalloc(i_size_tmp);
    snprintf(p_mac,i_size_tmp,"%s","00:ff:00:d5:44:a2");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_ip_proto);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}


void case4(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("add") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","add");

    //p_mac
    i_size_tmp = strlen("00:ff:00:d5:44:a2") + 1;
    p_mac = frmalloc(i_size_tmp);
    snprintf(p_mac,i_size_tmp,"%s","00:ff:00:d5:44:a2");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_ip_proto);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}


void case5(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("add") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","add");

    //p_ip_proto
    i_size_tmp = strlen("tcp") + 1;
    p_ip_proto = frmalloc(i_size_tmp);
    snprintf(p_ip_proto,i_size_tmp,"%s","tcp");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_ip_proto);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}

void case6(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("del") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","del");

    //p_ip_proto
    i_size_tmp = strlen("tcp") + 1;
    p_ip_proto = frmalloc(i_size_tmp);
    snprintf(p_ip_proto,i_size_tmp,"%s","tcp");

    //p_src_iprange_portrange
    i_size_tmp = strlen("192.168.1.2-192.168.1.30|80-100") + 1;
    p_src_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_src_iprange_portrange,i_size_tmp,"%s","192.168.1.2-192.168.1.30|80-100");

    //p_dst_iprange_portrange
    i_size_tmp = strlen("192.168.0.2-192.168.0.30|80-100") + 1;
    p_dst_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_dst_iprange_portrange,i_size_tmp,"%s","192.168.0.2-192.168.0.30|80-100");

    //p_mac
    i_size_tmp = strlen("00:ff:00:d5:44:a2") + 1;
    p_mac = frmalloc(i_size_tmp);
    snprintf(p_mac,i_size_tmp,"%s","00:ff:00:d5:44:a2");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_ip_proto);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}

void case7(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("del") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","del");

    //p_src_iprange_portrange
    i_size_tmp = strlen("192.168.1.2-192.168.1.30|80-100") + 1;
    p_src_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_src_iprange_portrange,i_size_tmp,"%s","192.168.1.2-192.168.1.30|80-100");

    //p_dst_iprange_portrange
    i_size_tmp = strlen("192.168.0.2-192.168.0.30|80-100") + 1;
    p_dst_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_dst_iprange_portrange,i_size_tmp,"%s","192.168.0.2-192.168.0.30|80-100");

    //p_mac
    i_size_tmp = strlen("00:ff:00:d5:44:a2") + 1;
    p_mac = frmalloc(i_size_tmp);
    snprintf(p_mac,i_size_tmp,"%s","00:ff:00:d5:44:a2");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}

void case8(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("del") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","del");

    //p_ip_proto
    i_size_tmp = strlen("tcp") + 1;
    p_ip_proto = frmalloc(i_size_tmp);
    snprintf(p_ip_proto,i_size_tmp,"%s","tcp");

    //p_src_iprange_portrange
    i_size_tmp = strlen("192.168.1.2-192.168.1.30|80-100") + 1;
    p_src_iprange_portrange = frmalloc(i_size_tmp);
    snprintf(p_src_iprange_portrange,i_size_tmp,"%s","192.168.1.2-192.168.1.30|80-100");

    //p_mac
    i_size_tmp = strlen("00:ff:00:d5:44:a2") + 1;
    p_mac = frmalloc(i_size_tmp);
    snprintf(p_mac,i_size_tmp,"%s","00:ff:00:d5:44:a2");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_ip_proto);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}


void case9(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("del") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","del");

    //p_mac
    i_size_tmp = strlen("00:ff:00:d5:44:a2") + 1;
    p_mac = frmalloc(i_size_tmp);
    snprintf(p_mac,i_size_tmp,"%s","00:ff:00:d5:44:a2");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_ip_proto);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}


void case10(void)
{
    int i_ret = 0;
    char *p_action = NULL;
    char* p_ip_proto = NULL;
    char *p_src_iprange_portrange = NULL;
    char *p_dst_iprange_portrange = NULL;
    char *p_mac = NULL;
    int i_size_tmp = 0;

    g_p_server = "127.0.0.1";
    g_i_port = 65535;
    //p_action
    i_size_tmp = strlen("del") + 1;
    p_action = frmalloc(i_size_tmp);
    snprintf(p_action,i_size_tmp,"%s","del");

    //p_ip_proto
    i_size_tmp = strlen("tcp") + 1;
    p_ip_proto = frmalloc(i_size_tmp);
    snprintf(p_ip_proto,i_size_tmp,"%s","tcp");

    i_ret = frcmd_filter(p_action, p_ip_proto, p_src_iprange_portrange, p_dst_iprange_portrange, p_mac);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    frfree(p_action);
    frfree(p_ip_proto);
    frfree(p_src_iprange_portrange);
    frfree(p_dst_iprange_portrange);
    frfree(p_mac);

}


int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t_frcmd_filter 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);

   	CU_add_test(pSuite, "case2", case2);

   	CU_add_test(pSuite, "case3", case3);

   	CU_add_test(pSuite, "case4", case4);

    CU_add_test(pSuite, "case5", case5);

    CU_add_test(pSuite, "case6", case6);

    CU_add_test(pSuite, "case7", case7);

    CU_add_test(pSuite, "case8", case8);

    CU_add_test(pSuite, "case9", case9);

    CU_add_test(pSuite, "case10", case10);
    return 0;

}

int main(int argc, char **argv)
{
    frmem_init();
    run_cunit(add_suite);
    frshow_leak();
    frmem_destroy();
    return 0;
}